package manage.controller;

import com.sun.org.apache.xpath.internal.objects.XObject;
import manage.base.BaseController;
import manage.dao.AttendanceMapper;
import manage.dao.UserMapper;
import manage.model.Attendance;
import manage.model.Attendancesub;
import manage.model.User;
import manage.service.AttendanceService;
import manage.service.impl.AttendanceServiceImpl;
import manage.service.impl.AttendancesubServiceImpl;
import manage.util.DateUtil;
import manage.util.ExcelUtil;
import manage.util.Msg;
import manage.util.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.http.HttpRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

@Controller
public class AttendanceController {

    @Autowired
    private AttendanceServiceImpl attendanceService;
    @Autowired
    private AttendancesubServiceImpl attendancesubService;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/attendance")
    @ResponseBody
    public Msg selectAll(HttpServletRequest request, Authentication authentication) {
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        User user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        String value = request.getParameter("value");
        String date = request.getParameter("date");
        if (value != null && !value.equals("") && value != "undefined") {
            return Msg.success().add("data", attendanceService.selectAny(page, limit, value));

        } else if (date != null && date != "") {
            return Msg.success().add("data", attendanceService.selectAll(page, limit, user.getAccount(), date));
        }

        return Msg.success().add("data", attendanceService.selectAll(page, limit));
    }

    @RequestMapping("/attendanceSub")
    @ResponseBody
    public Msg selectSubAll(HttpServletRequest request) {
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        return Msg.success().add("data", attendancesubService.selectAll(page, limit));
    }

    @ResponseBody
    @RequestMapping("/addAttendance")
    public Msg addAttendance(Attendance attendance, Authentication authentication) {
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        User user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        attendance.setOperatorId(user.getAccount());
        String id = attendance.getUserId() + DateUtil.getDate(attendance.getDateRecord());
        Attendancesub attendancesub = attendancesubService.select(id);
        if (attendancesub == null) {
            attendancesub = new Attendancesub();
            attendancesub.setId(id);
            attendancesub.setUserId(attendance.getUserId());
            attendancesub.setDate(DateUtil.getDate(attendance.getDateRecord()));
            System.out.println(attendancesub + "," + attendance);
            attendancesub = DateUtil.setSub(attendancesub, attendance);
            attendancesubService.insert(attendancesub);
        } else {
            attendancesub = DateUtil.setSub(attendancesub, attendance);
            attendancesubService.update(attendancesub);
        }
        String s = attendanceService.insert(attendance);
        return Msg.attendanceAdd(s);
    }

    @RequestMapping("/myAttendance")
    @ResponseBody
    public Msg myAttendance(HttpServletRequest request, Authentication authentication) {
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        User user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        return Msg.success().add("data", attendanceService.selectAny(page, limit, user.getAccount()));
    }

    @RequestMapping("/myAttendancesub")
    @ResponseBody
    public Msg myAttendance1(HttpServletRequest request, Authentication authentication) {
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        User user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        return Msg.success().add("data", attendancesubService.selectAll(page, limit));
    }
}


